package apiWeb

import (
	role "ZHONGYIHANGYAN/business/role"
	"fmt"

	"ZHONGYIHANGYAN/web"

	requset "ZHONGYIHANGYAN/bean/request"

	"github.com/gin-gonic/gin"
)

func (api *Api) RoleRouters(g *gin.RouterGroup) {
	routerG := g.Group("/role")
	{
		// 获取角色列表
		routerG.POST("/getRoleList", web.NewWebRouter(api.getRoleList).Func)
		// 添加或修改角色
		routerG.POST("/addOrUpdateRole", web.NewWebRouter(api.addOrUpdateRole).Func)
		// 删除角色
		routerG.POST("/deleteRole", web.NewWebRouter(api.deleteRole).Func)
	}
}

func (api *Api) InitRoleRouter() error {
	api.roleManage = role.Manage()
	api.web.RegRouters(api.RoleRouters)
	return nil
}

// 获取角色列表
func (api *Api) getRoleList(c *gin.Context, replayData *interface{}) error {
	query := requset.GetRoleListQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	infoList, err := api.roleManage.GetRoleList(&query)
	if err != nil {
		return err
	}
	*replayData = infoList
	return nil
}

// 添加或修改角色
func (api *Api) addOrUpdateRole(c *gin.Context, replayData *interface{}) error {
	info := requset.AddOrUpdateRoleQuery{}
	if err := api.web.GetPayload(c, &info); err != nil {
		return err
	}
	if err := api.roleManage.AddOrUpdateRole(&info); err != nil {
		return err
	}
	return nil
}

// 删除角色
func (api *Api) deleteRole(c *gin.Context, replayData *interface{}) error {
	query := requset.DeleteRoleQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	if len(query.RoleIds) == 0 {
		return fmt.Errorf("请传入需要删除的角色id")
	}
	if err := api.roleManage.DeleteRole(query.RoleIds); err != nil {
		return err
	}
	return nil
}
